对于每一行返回最小值的列名 |
您所在的位置:网站首页 › pandas 列名包含 › 对于每一行返回最小值的列名 |
使用列表理解来测试产品列表的最小值和最大值: #select all columns without first df1 = df.iloc[:, 1:] cols = df1.columns.to_numpy() df['most_sold'] = [cols[x].tolist() for x in df1.eq(df1.max(axis=1), axis=0).to_numpy()] df['least_sold'] = [cols[x].tolist() for x in df1.eq(df1.min(axis=1), axis=0).to_numpy()] print (df) id product1sold product2sold product3sold most_sold \ 0 1 2 3 3 [product2sold, product3sold] 1 2 0 0 5 [product3sold] 2 3 3 2 1 [product1sold] least_sold 0 [product1sold] 1 [product1sold, product2sold] 2 [product3sold] 如果性能不重要,可以使用DataFrame.apply: df1 = df.iloc[:, 1:] f = lambda x: x.index[x].tolist() df['most_sold'] = df1.eq(df1.max(axis=1), axis=0).apply(f, axis=1) df['least_sold'] = df1.eq(df1.min(axis=1), axis=0).apply(f, axis=1) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |